﻿using PBMessage;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class UIMatchBattle : BaseWidget {
	public GameObject Btn_Ready;
	public GameObject Btn_CancelReady;
	public UIGrid PlayersGrid;
	private List<MatchPlayerItem> playerList = new List<MatchPlayerItem>();

	public override void OnLoad()
	{
		Init();
	}
	
	public void Init()
	{
		UIEventListener.Get(Btn_Ready).onClick = Click_MatchBattleReady;
		UIEventListener.Get(Btn_CancelReady).onClick = Click_MatchBattleCnacelReady;
		SC_EnterReadyRoom MatchBattleReadyInfo = Game.Battle.MatchBattleReadyInfo;

        for (int i=0;i<Game.Battle.MatchBattleReadyInfo.PlayerList.Count;i++)
        {
            GameObject trans = NGUITools.AddChild(PlayersGrid.gameObject, Game.UI.GetPrefab("MatchPlayerItem"));
            MatchPlayerItem p = trans.GetComponent<MatchPlayerItem>();
            p.InitItem(MatchBattleReadyInfo.PlayerList[i]);
            playerList.Add(p);
        }
	}

	public void Refreh()
	{
		SC_SyncReadyRoomState ReadyRoomStateInfo = Game.Battle.ReadyRoomStateInfo;
		for(int i = 0; i < playerList.Count; i ++)
		{
			playerList[i].InitItem(ReadyRoomStateInfo.PlayerList[i]);
		}
	}

	public void Click_MatchBattleReady(GameObject go)
	{
		Game.Packet.SendMatchBattleReady(Game.Model.Get<PlayerModel>().Account, "NinJa", "default",true,2,Game.Battle.MatchBattleReadyInfo.RoomID);
	}

	public void Click_MatchBattleCnacelReady(GameObject go)
	{
		Game.Packet.SendMatchBattleReady(Game.Model.Get<PlayerModel>().Account, "NinJa", "default", false,2, Game.Battle.MatchBattleReadyInfo.RoomID);
	} 

	public void Server_MatchBattle(SC_SyncReadyRoomState data)
	{
		Game.Battle.ReadyRoomStateInfo = data;
		Refreh();
	}
}
